package com.myteam.fugaocrm.service;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.myteam.fugaocrm.modle.FgStaff;
import com.myteam.fugaocrm.modle.FgUser;
import com.myteam.fugaocrm.sqldb.mapper.FgStaffMapper;
import com.myteam.fugaocrm.sqldb.mapper.FgUserMapper;

@Service
public class LoginServiceImpl implements BaseService {

	@Resource protected FgUserMapper fgUserMapper;
	@Resource protected FgStaffMapper fgStaffMapper;
	
	public Map<String, String> login(String username, String password) {
		Map<String, String> result = new HashMap<String, String>();
		
		//校验
		if(username == null || password == null || "".equals(username) || "".equals(password)) {
			result.put("success", "0");
			result.put("msg", "用户名或密码不能为空");
			return result;
		}
		
		Pattern pattern = Pattern.compile("^[A-Za-z0-9_]{6,20}$");
		Matcher m1 = pattern.matcher(username); 
		Matcher m2 = pattern.matcher(password); 
		if( m1.matches() && m2.matches() ){ 
			//查询
			FgUser user = fgUserMapper.login(username, password);
			if(user != null) {
				try {
					FgStaff staff = fgStaffMapper.selectStaffByUserId(user.getUserId());
					result.put("success", "1");
//					result.put("staffId", Base64Util.encodeBase64(staff.getStaffId().toString()));
					result.put("staffId", staff.getStaffId().toString());
					result.put("staffName", staff.getStaffName());
					result.put("permission", staff.getPermission());
				} catch (Exception e) {
					e.printStackTrace();
				}
				
				return result;
			}  
	    } 

		result.put("success", "0");
		result.put("msg", "用户名或密码错误");
		return result;
	}
	
}
